/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package javax.servlet;

import java.io.IOException;

/** 
 *   初识Filter
 *
 *   Filter，即过滤器，在客户端和服务端间承担请求和响应过滤处理的角色。至于使用Filter的原因，开发的小伙伴可能已经达成共识，在Filter中，可以进行通用性的过滤处理。
 *
 *   客户端到服务端方向的过滤器，可以承担：URL权限校验及过滤、Http Method权限控制、请求数据过滤转换等等。只要是HttpServletRequest承载的信息，都可以作为过滤处理的依据。
 *
 *   服务端到客户端方向的过滤器，可以承担：过滤数据敏感词汇、编码响应数据、压缩响应信息、统一处理地址重定向等等。只要是HttpServletResponse承载的信息，都可以作为过滤处理的依据。
 *
 *   Filter流程
 *
 *   ① 客户端发送请求，HttpServletRequest（请求）到达Servlet之前，过滤器拦截到客户端的HttpServletRequest。
 *
 *   ② 根据业务需求，配合HttpServletRequest的数据项，进行过滤处理，此时也可以修改HttpServletRequest头信息和有效负载内容。
 *
 *   ③ 过滤器对HttpServletRequest处理完成之后，会调用doFilter方法，放行请求，此时请求会在过滤器链上向下传播，如果过滤器链已到末尾，那么请求会到达Servlet，Servlet对HttpServletRequest进行处理，同时产生HttpServletResponse并发送给客户端。
 *
 *   ④ HttpServletResponse（响应）到达客户端之前，过滤器拦截到响应的HttpServletResponse。
 *
 *   ⑤ 根据业务需求，配合HttpServletResponse的数据项，进行过滤处理，此时也可以修改HttpServletResponse头信息和有效负载内容。
 *
 *   ⑥ 最后，HttpServletResponse经传输到达客户端。
 *
 *
 * Filter生命周期：
 *
 * Servlet容器启动时，会创建Filter的实例对象，并调用其init方法，完成对象的初始化功能。Filter对象只创建一次，init方法也只会执行一次。
 * 
 *
 *
 * 过滤器是一个对象，它对资源的请求（servlet或静态内容）或来自资源的响应执行筛选任务，或同时执行这两个任务.
 *  * 过滤器在doFilter方法中执行过滤.
 *  * 每个过滤器都可以访问FilterConfig对象，从中可以获得初始化参数，ServletContext的引用可以用来加载筛选任务所需的资源.
 *
 *
 * A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both.
 * <br><br>
 * Filters perform filtering in the <code>doFilter</code> method. Every Filter has access to 
 * a FilterConfig object from which it can obtain its initialization parameters, a
 * reference to the ServletContext which it can use, for example, to load resources
 * needed for filtering tasks.
 * <p>
 * Filters are configured in the deployment descriptor of a web application
 * <p>
 * Examples that have been identified for this design are<br>
 * 1) Authentication Filters <br>
 * 2) Logging and Auditing Filters <br>
 * 3) Image conversion Filters <br>
 * 4) Data compression Filters <br>
 * 5) Encryption Filters <br>
 * 6) Tokenizing Filters <br>
 * 7) Filters that trigger resource access events <br>
 * 8) XSL/T filters <br>
 * 9) Mime-type chain Filter <br>
 * @since	Servlet 2.3
 */
 
public interface Filter {

	/**
	 *  由web容器调用，以向筛选器指示正在将其置于服务中。在实例化过滤器之后，servlet容器只调用init方法一次.
	 *      在要求筛选器执行任何筛选工作之前，init方法必须成功完成.
	 *
	 * Called by the web container to indicate to a filter that it is being placed into
	 * service. The servlet container calls the init method exactly once after instantiating the
	 * filter. The init method must complete successfully before the filter is asked to do any
	 * filtering work. <br><br>
	 * The web container cannot place the filter into service if the init method either<br>
	 * 1.Throws a ServletException <br>
	 * 2.Does not return within a time period defined by the web container
	 */
	public void init(FilterConfig filterConfig) throws ServletException;
	
	
	/**
	 * 每次由于客户机请求链末端的资源而使请求/响应对通过链时，容器都会调用筛选器的doFilter方法.
	 *      传入此方法的FilterChain允许筛选器将请求和响应传递给链中的下一个实体.
	 *
	 * The <code>doFilter</code> method of the Filter is called by the container
	 * each time a request/response pair is passed through the chain due
	 * to a client request for a resource at the end of the chain. The FilterChain passed in to this
	 * method allows the Filter to pass on the request and response to the next entity in the
	 * chain.<p>
	 * A typical implementation of this method would follow the following pattern:- <br>
	 * 1. Examine the request<br>
	 * 2. Optionally wrap the request object with a custom implementation to
	 * filter content or headers for input filtering <br>
	 * 3. Optionally wrap the response object with a custom implementation to
	 * filter content or headers for output filtering <br>
	 * 4. a) <strong>Either</strong> invoke the next entity in the chain using the FilterChain object (<code>chain.doFilter()</code>), <br>
	 ** 4. b) <strong>or</strong> not pass on the request/response pair to the next entity in the filter chain to block the request processing<br>
	 ** 5. Directly set headers on the response after invocation of the next entity in the filter chain.
	 **/
    public void doFilter ( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException;

	/**
	 *  由web容器调用，以向筛选器指示正在停止服务。只有在筛选器的doFilter方法中的所有线程都已退出或在超时时间过后，才会调用此方法.
	 *      * 在web容器调用此方法后，它将不再在此筛选器实例上调用doFilter方法.
	 *
	 * Called by the web container to indicate to a filter that it is being taken out of service. This
	 * method is only called once all threads within the filter's doFilter method have exited or after
	 * a timeout period has passed. After the web container calls this method, it will not call the
	 * doFilter method again on this instance of the filter. <br><br>
	 *
	 * This method gives the filter an opportunity to clean up any resources that are being held (for
	 * example, memory, file handles, threads) and make sure that any persistent state is synchronized
	 * with the filter's current state in memory.
	 */

	public void destroy();


}

